package org.dromara.system.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe;
import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.system.domain.GatewayReport;
import org.dromara.system.domain.pojo.EventPropertyPostRequest;
import org.dromara.system.domain.pojo.Param;
import org.dromara.system.domain.pojo.Property;
import org.dromara.system.service.IDeviceService;
import org.dromara.system.service.IGatewayReportService;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author xmac
 * @Description TODO
 * @Date 2024/5/14 10:01
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MqttClientSubscribeListener {


	@MqttClientSubscribe("/sys/thing/event/property/post/+")
	public void subLampTopic(String topic, byte[] payload) {
		String message = new String(payload, StandardCharsets.UTF_8);
		log.info("网关主题:{} payload:{}", topic, message);
		EventPropertyPostRequest eventPropertyPostRequest = JsonUtils.parseObject(message, EventPropertyPostRequest.class);
		List<Param> params = eventPropertyPostRequest.getParams();
		String value ="49";
		for (Param param : params) {
			if (Objects.equals(param.getClientID(), "002")){
				GatewayReport gatewayReport = new GatewayReport();
				gatewayReport.setName("80000001");
				List<Property> properties = param.getProperties();
				if (properties != null) {
					for (Property property : properties) {
						if ("IoInputStatus".equals(property.getName())) {
							value =property.getValue();
						}
					}
				}
				gatewayReport.setValue((char) Integer.parseInt(value));
				IGatewayReportService bean = SpringUtils.getBean(IGatewayReportService.class);
				bean.insertReport(gatewayReport);

			}
		}
	}




}
